package com.fts.admin.record.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fts.admin.record.convert.RecordPoConvertVo;
import com.fts.admin.record.entity.dto.RecordDTO;
import com.fts.admin.record.entity.po.RecordPO;
import com.fts.admin.record.entity.vo.RecordVO;
import com.fts.admin.record.mapper.RecordMapper;
import com.fts.admin.record.service.RecordService;
import com.fts.common.query.BaseQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author lzt
 * @date 2023/11/21 23:51
 */
@Service
@Slf4j
public class RecordServiceImpl extends ServiceImpl<RecordMapper, RecordPO> implements RecordService {

    @Resource
    private RecordMapper recordMapper;

    @Override
    public RecordVO save(RecordDTO recordDTO) {
        RecordPO recordPO = new RecordPO();
        BeanUtils.copyProperties(recordDTO,recordPO);
        super.save(recordPO);
        RecordPO re = super.getById(recordPO.getId());
        RecordVO recordVO = RecordPoConvertVo.INSTANCE.recordPoToVo(re);
        return recordVO;
    }

    @Override
    public boolean delete(Long id) {
        return super.removeById(id);
    }

    @Override
    public IPage<RecordVO> getPage(BaseQuery baseQuery) {
        IPage<RecordVO> recordVOIPage = new Page<>();
        QueryWrapper<RecordPO> queryWrapper = new QueryWrapper<>();
        if (null != baseQuery.getType()){
            queryWrapper.eq("type",baseQuery.getType());
        }
        IPage<RecordPO> recordPOPage = recordMapper.selectPage(new Page<>(baseQuery.getCurrent(), baseQuery.getSize()), queryWrapper);
        BeanUtils.copyProperties(recordPOPage,recordVOIPage);
        return recordVOIPage;
    }
}
